# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:

# s = '''orgId=2000000000081408&PART_OLDCODE=&PART_CNAME=&PART_CODE=&PRODUCE_FAC=&LACK_STATE=&IN_PLAN=&PART_SORT=&SELLPART_PROPERTY=&FRAME_ID=&SALE_PRICE=&LC=&CAR_TYPE=&IS_RECOVER=&REMARK=&STATE=10011001&ASSEMBLY_LEVEL=&PRODUCE_STATE=&buyerId=&ARTICLES_TYPE=&IS_OIL=&IS_SPECIAL=&IS_TIME=&orderCol=&order=&_='''
#
# for par in s.split("&"):
#     key = par.split("=")[0]
#     value = par.split("=")[1]
#
#     print('{}="{}",'.format(key, value))
#
# import re
# def quote_keys_for_json(json_str):
#     """给键值不带双引号的json字符串的所有键值加上双引号。
#     注：解析一般的不严格的json串 速度比标准库要慢。"""
#     quote_pat = re.compile(r'".*?"')
#     a = quote_pat.findall(json_str)
#     json_str = quote_pat.sub('@', json_str)
#     key_pat = re.compile(r'(\w+):')
#     json_str = key_pat.sub(r'"\1":', json_str)
#     assert json_str.count('@') == len(a)
#     count = -1
#     def put_back_values(match):
#         nonlocal count
#         count += 1
#         return a[count]
#     json_str = re.sub('@', put_back_values, json_str)
#     return json_str
#
# s = '''{a: "123", b: "456"}'''
# import json
# r = json.loads(quote_keys_for_json(s))
# for i in r:
#     print(r[i])


# keys = ["a", "b", "c"]
# d = {
#     "a": 1,
#     "b": 2,
#     "c": 3,
#     "d": 4
# }
# _dict = {k: d[k] for k in keys}
# print(_dict)


# import pymongo
#
# db = pymongo.MongoClient(host='192.168.10.222', port=27017)['changan_local']
# col = db["carCode"]
# col2 = db["carCode_clean"]
# data = col.find()
# num = 0
# for i in data:
#     carName = i.get("carModel")
#     carCode = i.get("carCode")
#     isOld = i.get("isold")
#     carTail = i.get("carTail")
#     carTail_l = carTail.split("|")
#     carCode_fina = list(filter(lambda x: '.' in x, carTail_l))[0]
#     # print(carCode_fina)
#     carTail_l.remove(carCode_fina)
#     carTail_fina = '|'.join(carTail_l)
#     # print(carCode_fina, carTail_fina)
#     _dict = {
#         "carName": carName.strip(),
#         "carCode": carCode.strip(),
#         "carTail_fina": carTail_fina.strip(),
#         "carCode_fina": carCode_fina.strip(),
#         "isOld": isOld
#     }
#     col2.insert(_dict)

# from _set import _set
#
# for i in range(20):
#     if i in _set:
#         continue
#     _set.append(i)
#     print(i)
# with open('./_set.py', 'w') as s:
#     s.write("_set = {}".format(_set))


# with open('./devtools.json', 'r') as l:
#     res = l.read()
#
# import re
# Authorization = re.findall(r'"Authorization": "(.*?)"', res)[0]
# print(Authorization)
import requests
# cookiestr = '1fj34l3b33dyeqs7jjioetid2'
# cookiestr = '1infia02s454h1kxsknoern17k'
# cookiestr = '14h6ag6lj3nyh8bxbkvij3d6o'   # 对
# Authorization = '3bfddf83b6ed42c9b04b6fa3266629bf'  # 对
# Authorization = 'ab9b729691af45b0a0b56eb8374bd5a9'
# def checkCookies(Authorization, cookiestr):
#     url = '''http://epc.changan.com.cn:8080/system/bus-style/listBusStyleByBrandId'''
#     # url = '''http://epc.changan.com.cn:8080/front/knowledge.html'''
#     headers = {
#         "Host": "epc.changan.com.cn:8080",
#         "Upgrade-Insecure-Requests": "1",
#         "Authorization": Authorization,
#         "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
#         "Referer": "http://epc.changan.com.cn:8080/front/index.html",
#         "Cookie": "JSESSIONID={}".format(cookiestr),
#         # "": "",
#     }
#     postData = {
#         "brandId": "474549e1fee84369913723719d5711f2"
#     }
#     response = requests.post(url=url, headers=headers, data=postData, allow_redirects=False)
#     print(response.status_code)
#     if response.status_code != 200:
#         return False
#     return True
#
# print(checkCookies(Authorization, cookiestr))


import base64
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA

publicKey = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrWhB6f7MQIg51sULk0mQi6WrEv1ZjkbYr0sXp1b2oe8fT3WkkKurYBec05bGKAm02zSB7OBNfs2qVyMgRKUZNmqGvT+wMvQS0lfhXisXYqu+aRWPYatud+Y40aURjdui+sqlFIVLEdvbjLqnEhielTIoCaNULwmgCLdPOjZoJvwIDAQAB\n-----END PUBLIC KEY-----'

username = 'JS00008A'
password = 'wh12345'

def encrypt_(username, password, publicKey=None):
    if not publicKey:
        publicKey = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrWhB6f7MQIg51sULk0mQi6WrEv1ZjkbYr0sXp1b2oe8fT3WkkKurYBec05bGKAm02zSB7OBNfs2qVyMgRKUZNmqGvT+wMvQS0lfhXisXYqu+aRWPYatud+Y40aURjdui+sqlFIVLEdvbjLqnEhielTIoCaNULwmgCLdPOjZoJvwIDAQAB\n-----END PUBLIC KEY-----'
    rsakey = RSA.importKey(publicKey)
    cipher = PKCS1_v1_5.new(rsakey)
    encryped_username = base64.b64encode(cipher.encrypt(username.encode()))
    encryped_password = base64.b64encode(cipher.encrypt(password.encode()))
    return [encryped_username, encryped_password]

# u,p = encrypt_(username, password)
# print(u)
# print(p)

# headers = {
#     "Host": "epc.changan.com.cn:8080",
#     "Origin": "http://epc.changan.com.cn:8080",
#     "X-Requested-With": "XMLHttpRequest",
#     "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
#     "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
#     "Referer": "http://epc.changan.com.cn:8080/front/login.html",
# }
# cookies = {
#     "JSESSIONID": "i0iwro1sfaxw1h8hp84doef6d"
# }
# postData = {
#     "account": u,
#     "password": p,
#     "identifyingCode": "tdhz",
#     "identifyingId": "1550404441481905",
# }
# url = "http://epc.changan.com.cn:8080/system/sys-dealer/login"
# response = requests.post(url=url, headers=headers, cookies=cookies, data=postData)
# print("Xxxxxx")

import time
from untils.yun_da_ma import code_verificate
from setting import *

def login():
    headers = {
        "Host": "epc.changan.com.cn:8080",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
        # "": "",
    }
    loginUrl = "http://epc.changan.com.cn:8080/front/login.html"
    res1 = requests.get(url=loginUrl, headers=headers, allow_redirects=False, timeout=30)
    with open('./login.html', 'w') as f:
        f.write(res1.content.decode())
    cookies = res1.cookies.get_dict()
    print(cookies)

    headers["Origin"] = "http://epc.changan.com.cn:8080"
    headers["Referer"] = "http://epc.changan.com.cn:8080/front/login.html"
    url2 = "http://epc.changan.com.cn:8080/system/sys-slide-show/selectSlideShow"
    _ = requests.post(url=url2, headers=headers, data={})
    cookies = _.cookies.get_dict()
    print(cookies)

    # 请求验证码
    identifyingId = str(time.time()).replace('.', '')
    codeUrl = 'http://epc.changan.com.cn:8080/kaptcha/identifyingCode?/identifyingId={}'.format(identifyingId)
    codeRes = requests.get(url=codeUrl, headers=headers, cookies=cookies)
    with open('./code.png', 'wb') as c:
        c.write(codeRes.content)
    # cookies = codeRes.cookies.get_dict()
    # print(cookies)
    rs = code_verificate('./code.png', ydm_username, ydm_password, ydm_appId, ydm_appKey, code_type=1006)
    verifyCode = rs[0]

    headers = {
        "Host": "epc.changan.com.cn:8080",
        "Origin": "http://epc.changan.com.cn:8080",
        "X-Requested-With": "XMLHttpRequest",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Referer": "http://epc.changan.com.cn:8080/front/login.html",
    }
    loginUrl = "http://epc.changan.com.cn:8080/system/sys-dealer/login"
    postData = {
        "account": encrypt_(username, password)[0],
        "password": encrypt_(username, password)[1],
        "identifyingCode": verifyCode,
        "identifyingId": identifyingId,
    }
    response = requests.post(url=loginUrl, headers=headers, cookies=cookies, data=postData)
    with open('./index.html', 'wb') as c:
        c.write(response.content)
    print(response.content.decode())

# login()

s = '''Host: service.citroen.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://service.citroen.com/docprAC/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: BIGipServerNEWAPVPROI_OIT.app~NEWAPVPROI_OIT_pool=496009226.20480.0000; PSACountry=CN; CodeLanguePaysOI=zh_CN; PGAN65374651=10; DISPLAYDOCAN65374651=1; _pk_id.78.196e=b6d3675882a9c8ed.1551104401.4.1551157395.1551157395.; _pk_ses.66.196e=*; JSESSIONID=0000GIDd_LoqHlOiR2y5sdFvVip:1c0on7jcr; _pk_id.66.196e=da231deb4d00e588.1551084352.8.1551186255.1551184494.
Connection: keep-alive'''

s_l = s.split("\n")
for i in s_l:
    k = i.split(": ")[0]
    v = i.split(": ")[1]
    print('"{}": "{}",'.format(k, v))

# s = '1550407605175249'
# print(s)
# print(str(time.time()).replace('.', ''))


